package com.linq.cool.beauty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linq.cool.beauty.domain.BeautyServiceStore;
import com.linq.cool.beauty.dto.BeautyServiceStoreQueryDTO;
import com.linq.cool.beauty.mapper.BeautyServiceStoreMapper;
import com.linq.cool.beauty.service.BeautyServiceStoreService;
import com.linq.cool.common.constants.Constants;
import com.linq.cool.common.core.exception.BusinessException;
import org.springframework.stereotype.Service;

/**
 * @Author: yqlin
 * @Date: 2021/2/13 22:15
 * @Description:
 * @Version: 1.0.0
 */

@Service
public class BeautyServiceStoreServiceImpl extends ServiceImpl<BeautyServiceStoreMapper, BeautyServiceStore> implements BeautyServiceStoreService {

    @Override
    public IPage<BeautyServiceStore> listByPage(IPage<BeautyServiceStore> page, BeautyServiceStoreQueryDTO dto) {
        return this.page(page, this.getLambdaQueryWrapper(dto));
    }

    @Override
    public boolean saveServiceStore(BeautyServiceStore serviceStore) {
        // 名称唯一
        if (this.checkLNamerUnique(serviceStore)) {
            throw new BusinessException("修改店面'" + serviceStore.getName() + "'失败，名称已存在");
        }
        return this.save(serviceStore);
    }

    @Override
    public boolean updateServiceStore(BeautyServiceStore serviceStore) {
        // 名称唯一
        if (this.checkLNamerUnique(serviceStore)) {
            throw new BusinessException("修改店面'" + serviceStore.getName() + "'失败，名称已存在");
        }
        return this.updateById(serviceStore);
    }

    /**
     * 名称唯一
     *
     * @param serviceStore
     *
     * @return
     */
    private boolean checkLNamerUnique(BeautyServiceStore serviceStore) {
        Long serviceStoreId = null == serviceStore.getId() ? -1L : serviceStore.getId();
        BeautyServiceStore one = this.getOne(new LambdaQueryWrapper<BeautyServiceStore>()
                                                     .select(BeautyServiceStore::getId, BeautyServiceStore::getName)
                                                     .eq(BeautyServiceStore::getName, serviceStore.getName())
        );
        // 判断id是否一致 不一致就代表 唯一 false
        return null != one && !serviceStoreId.equals(one.getId());
    }


    private LambdaQueryWrapper<BeautyServiceStore> getLambdaQueryWrapper(BeautyServiceStoreQueryDTO dto) {
        return new LambdaQueryWrapper<BeautyServiceStore>()
                .like(StringUtils.checkValNotNull(dto.getName()), BeautyServiceStore::getName, dto.getName())
                .like(StringUtils.checkValNotNull(dto.getContactName()), BeautyServiceStore::getContactName, dto.getContactName())
                .like(StringUtils.checkValNotNull(dto.getContactPhone()), BeautyServiceStore::getContactPhone, dto.getContactPhone())
                .between(StringUtils.checkValNotNull(dto.getParams().get(Constants.BEGIN_TIME)), BeautyServiceStore::getGmtCreate, dto.getParams().get(Constants.BEGIN_TIME), dto.getParams().get(Constants.END_TIME))
                ;
    }
}
